/**
 * @(#)NewTaskTImer.java 2016年11月17日
 * Copyright(C) 2016 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.timer;

import org.jsbd.boss.dao.IChannelApkConfigDAO;
import org.jsbd.boss.domian.channel.ChannelApkConfig;
import org.jsbd.boss.service.IApkProvinceService;
import org.jsbd.boss.service.IApkSimulatorService;
import org.jsbd.boss.service.INewTaskService;
import org.jsbd.boss.util.CacheKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 新增任务timer
 *
 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
 * @version 1.0.0
 */
@Service
public class NewTaskTimer extends TaskAdapterHandler {
    public static final Logger LOGGER = LoggerFactory.getLogger(NewTaskTimer.class);

    @Resource
    private IApkProvinceService apkProvinceService;

    @Resource(name = "newTaskService")
    private INewTaskService newTaskService;

    @Resource
    private IApkSimulatorService apkSimulatorService;

    @Resource
    private IChannelApkConfigDAO channelApkConfigDAO;

    /*
     * (non-Javadoc)
     *
     * @see org.jsbd.boss.timer.TaskHandler#execute()
     */
    @Override
    public void run() {
        long taskTime = 30;
        long configTime = 30;

        Object refreshTime = cacheParamManager.getParamValue(CacheKey.NEW_TASK_REFRESH);
        if (refreshTime != null) {
            taskTime = Long.parseLong(refreshTime.toString());
        }
        if (redisClient.evalSetNx(CacheKey.NEW_TIMER_LOCK_KEY + 1, taskTime)) {
            updateApkOfProvince();
            updateApkInfos();
        }
        Object apkTimeStr = cacheParamManager.getParamValue(CacheKey.APK_CONFIG_TIME);
        if (apkTimeStr != null) {
            configTime = Long.parseLong(apkTimeStr.toString());
        }
        if (redisClient.evalSetNx(CacheKey.NEW_TIMER_LOCK_KEY + 2, configTime)) {
            updateApkVer();
            updateNewTask();
        }


    }


    private void updateApkInfos() {
        List<ChannelApkConfig> list = channelApkConfigDAO.selectApkInfos(null);
        if (list != null && list.size() > 0) {
            for (ChannelApkConfig config : list) {
                String key = CacheKey.APK_KEY + config.getMark();
                redisClient.put(key, config, 2 * 60);
            }
        }
    }

    /**
     * updateApkVer:更新apk的版本号 5分钟更新一次
     *
     * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
     */
    private void updateApkVer() {
        try {
            long now = System.currentTimeMillis();
            apkSimulatorService.updateApkVers();
            LOGGER.warn("更新apk的版本号 {} 秒", ((System.currentTimeMillis() - now) / 1000));
        } catch (Exception e) {
            LOGGER.error("更新apk的版本号", e);
        }
    }

    /**
     * updateProvince:更新查询省份可用apk缓存 13秒
     *
     * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
     */
    private void updateApkOfProvince() {
        try {
            long now = System.currentTimeMillis();
            apkProvinceService.updateApksOfProvinces();
            LOGGER.warn("更新查询省份 {} 秒", ((System.currentTimeMillis() - now) / 1000));
        } catch (Exception e) {
            LOGGER.error("更新查询省份异常", e);
        }
    }


    /**
     * updateNewTask:apk可执行总量 5分钟一次
     *
     * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
     */
    private void updateNewTask() {
        try {
            long now = System.currentTimeMillis();
            newTaskService.updateTaskOfCache();
            LOGGER.warn("新增任务定时器 {} 秒", ((System.currentTimeMillis() - now) / 1000));
        } catch (Exception e) {
            LOGGER.error("新增任务定时器异常", e);
        }
    }

}
